package com.sky.zzh;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.next.app.StandardApplication;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.sky.zzh.home.LoginActivity;
import com.sky.zzh.util.AlarmReceiver;
import com.sky.zzh.util.UserInfoManager;

public class SHApplication extends StandardApplication {

	private ArrayList<Activity> activity_list = new ArrayList<Activity>();
	private BaseActivity currentActivity;
	private static SHApplication application;

	public boolean isLocked = false;
	LockScreenReceiver receiver;
	IntentFilter filter;
	private Calendar c = null;

	public BaseActivity getCurrentActivity() {
		return currentActivity;
	}

	public void setCurrentActivity(BaseActivity currentActivity) {
		this.currentActivity = currentActivity;
	}

	public ArrayList<Activity> getActivity_list() {
		return activity_list;
	}

	public void onCreate() {
		super.onCreate();
		application = this;
		initImageLoader(this);
		receiver = new LockScreenReceiver();
		filter = new IntentFilter();
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		this.registerReceiver(receiver, filter);
		c = Calendar.getInstance();
		if (UserInfoManager.getInstance().isNaozhong()) {
			startNaozhong();
		}
	}

	public void startNaozhong() {
		Log.i("TAG", "setTime");

		AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//		Intent intent = new Intent(this,AlarmReceiver.class);
//		PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
//				PendingIntent.FLAG_CANCEL_CURRENT);
//		am.cancel(sender);

		long timeInMillis = 0;
		// String time =
		// mPreference.getString(Constant.Task.KEY_TASK_SUMMARY_ALARM_TIME,"22:00");
		Calendar c = Calendar.getInstance();
		// c.add(Calendar.SECOND, 5);
		String[] splits = UserInfoManager.getInstance().getNaozhongTime().split(":");
		int hour = Integer.parseInt(splits[0]);
		int minute = Integer.parseInt(splits[1]);
		c.set(Calendar.HOUR_OF_DAY, hour);
		c.set(Calendar.MINUTE, minute);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);

		Calendar current = Calendar.getInstance();
		while (current.getTimeInMillis() > c.getTimeInMillis()) {
			c.add(Calendar.DAY_OF_MONTH, 1);
		}
		timeInMillis = c.getTimeInMillis();
		Intent intent2 = new Intent(this,AlarmReceiver.class);
		PendingIntent sender2 = PendingIntent.getBroadcast(this, 0, intent2, 0);
		am.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis, AlarmManager.INTERVAL_DAY, sender2);
	}

	public void cancelNaozhong() {
		Intent intent = new Intent(this, AlarmReceiver.class);
		PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
		AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
		// 取消警报
		am.cancel(pi);
	}

	public void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).writeDebugLogs() // Remove
																																		// for
																																		// release
																																		// app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}

	public static Context getContext() {
		return application;
	}

	public void addActivity(Activity a) {
		activity_list.add(a);

	}

	/**
	 * 
	 */
	public void exitApplication() {
		for (Activity a : activity_list) {
			a.finish();
		}
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	public void onlyLogin() {
		for (Activity a : activity_list) {
			if (!(a instanceof LoginActivity)) {
				a.finish();
			}
		}
	}

	/**
	 * 
	 * @return
	 */
	public static SHApplication getInstance() {

		return (SHApplication) StandardApplication.getInstance();
	}

	class LockScreenReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			/* 在这里处理广播 */
			if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
				isLocked = true;
			}
		}
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		this.unregisterReceiver(receiver);
	}
}
